﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DelonixRegiaHotel.Classes;
using DelonixRegiaHotel.DBManager;

namespace DelonixRegiaHotel
{
    public partial class CreateDuty : Form
    {
        public CreateDuty()
        {
            InitializeComponent();
            string temp = "Du";
            tbxDutyTypeId.Text = generateID(temp);         
        }

        private void btnCancelNewDuty_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //generate auto dutyTypeId
        public string generateID(string dutyTypeID)
        {
            dutyTypeID += DateTime.Now.GetHashCode();
            return dutyTypeID;
        }

        private void btnCreateDuty_Click(object sender, EventArgs e)
        {
            Duty newDuty = new Duty();
            newDuty.DutyTypeID = tbxDutyTypeId.Text;
            newDuty.DutyName = tbxDutyName.Text;
            newDuty.DutyDescription = tbxDutyDesc.Text;

            if (tbxDutyName.Text.Equals("") || tbxDutyDesc.Text.Equals(""))
            {
                MessageBox.Show("All forms have to be filled in");
                return;
            }

            //check if duty already exist in database
            if (StaffAndDutyDBManager.GetDutyByName(newDuty) == true)
            {
                MessageBox.Show("Duty already exist");
                return; // force stop this method. if no return, it will continue running the code below.
            }

            else
            {
                int success = StaffAndDutyDBManager.CreateDuty(newDuty);
                if (success == 1)
                {
                    MessageBox.Show("Duty is created!");
                }
            }

            ReloadForm();
        }

        //Reload Form after button click
        private void ReloadForm()
        {
            tbxDutyName.ResetText();
            tbxDutyDesc.ResetText();
            string temp = "Du";
            tbxDutyTypeId.Text = generateID(temp);
        }

        }
    }

    